﻿using Fish.Controllers;
using Fish.DAL;
using Fish.Models;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using WebMatrix.WebData;

namespace Fish
{
    public class MvcApplication : System.Web.HttpApplication
    
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);

            //WebSecurity.InitializeDatabaseConnection("Data Source=(localdb)\v11.0;AttachDbFilename=\"C:\\Users\\omar\\Dropbox\\Cloud Project\\Fish\\Fish\\App_Data\\FishContext-20131206182049.mdf\";Initial Catalog=FishContext-20131206182049;Integrated Security=True;MultipleActiveResultSets=True","Users","ID","Username",true);
        }

        protected void Session_Start(object sender, EventArgs e)
        {
            if (User.Identity.IsAuthenticated)
            {
                AccountController ac = new AccountController();
                string username = User.Identity.Name;
                System.Diagnostics.Debug.WriteLine("USERNAME: " + username);
                FishContext db = new FishContext();
                Users users = db.Users.Where(u => u.Username.Equals(username)).FirstOrDefault();
                if (users == null)
                {
                        ac.LogOff();
                }
                else
                {
                    System.Diagnostics.Debug.WriteLine("USErID: " + users.ID);
                    Session["userID"] = users.ID;
                }
            }
        }

    }
}
